package cn.dnui_lf_705.dnui_16110100705.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.UserBean;
import cn.dnui_lf_705.dnui_16110100705.controller.AlterUserInfoActivity;
import cn.dnui_lf_705.dnui_16110100705.controller.BuyActivity;
import cn.dnui_lf_705.dnui_16110100705.controller.CommentInfoActivity;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.UserModel;


/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment implements OnRetrofitListener<UserBean> {
    private ImageView blurImageView,avatarImageView;
    private RecyclerView recyclerView;
    private TextView username, userpass,phone,address,comment;
    private View view;
    private ImageView userpic,scan,user_details;
    private UserBean userBean;
    private LinearLayout dingdan,myComment,userUpdata,myTwoCode,setting,user,shoucang;
    public MeFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.tab04, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.view=view;
        initViews();
        initEvents();
        initData();
    }

    @Override
    void initViews() {
        username=view.findViewById(R.id.h_name);
        phone=view.findViewById(R.id.h_tel);
        address=view.findViewById(R.id.h_address);
        userpic=view.findViewById(R.id.h_head_pic);
        user_details=view.findViewById(R.id.user_details);
        dingdan=view.findViewById(R.id.dingdan);
        myComment=view.findViewById(R.id.comment1);
        userUpdata=view.findViewById(R.id.userupdata);
        setting=view.findViewById(R.id.setting);
        user=view.findViewById(R.id.user);
        shoucang=view.findViewById(R.id.wodeshoucang);
    }

    @Override
    void initEvents() {
            //点击控件后进行跳转处理
        dingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到订单界面
                Intent intent = new Intent(getActivity(),BuyActivity.class);
                startActivity(intent);
            }
        });
        shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到收藏界面
                //Intent intent = new Intent(getActivity(),CollectActivity.class);
                //startActivity(intent);
            }
        });
        myComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到评论
                Intent intent = new Intent(getActivity(),CommentInfoActivity.class);
                startActivity(intent);
            }
        });
        userUpdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = String.valueOf(userBean.getUsername());
                String str2 = String.valueOf(userBean.getPassword());
                String str3 = String.valueOf(userBean.getMobilenum());
                String str4 = String.valueOf(userBean.getAddress());
                String str5 = String.valueOf(userBean.getComment());
                Intent intent =new Intent(getActivity(),AlterUserInfoActivity.class);
                intent.putExtra("username",str1);
                intent.putExtra("userpass",str2);
                intent.putExtra("phone",str3);
                intent.putExtra("address",str4);
                intent.putExtra("comment",str5);
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {
        UserModel userModel = new UserModel();
        userModel.getUserById(getUser_id(),this);
    }



    @Override
    public void onsuccess(UserBean userBean, int flag) {
            this.userBean = userBean;
            username.setText(userBean.getUsername());
            phone.setText(userBean.getMobilenum());
            address.setText(userBean.getAddress());
    }

    @Override
    public void onFailure(String string) {
            showtoast("获取用户信息失败");
    }
    @Override
    public void onResume(){
        super.onResume();
        initData();
    }
}
